package com.ktmall.ktmalluijava.controller;

import com.ktmall.ktmalluijava.pojo.Banner;
import com.ktmall.ktmalluijava.service.BannerService;
import com.ktmall.ktmalluijava.utils.Page;
import com.ktmall.ktmalluijava.utils.ResMsg;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/14 上午8:42
 */
@RestController
@RequestMapping("/banner")
public class BannerController {

    @Resource
    private BannerService bannerService;

    @GetMapping("/list")
    public String select(Integer type, Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 5 : pageSize;
        Page<Banner> page = bannerService.getPage(type,pageNo,pageSize);
        return ResMsg.success("轮播图列表加载成功",page);
    }

    @PostMapping("/add")
    public String add(@RequestBody Banner banner){
        int i = bannerService.add(banner);
        if(i>0){
            return ResMsg.success("新增成功");
        }else{
            return ResMsg.error("新增失败");
        }
    }

    @PostMapping("/del")
    public String delete(@RequestBody Integer... id){
        int i = bannerService.delete(id);
        if(i>0){
            return ResMsg.success("删除成功");
        }else{
            return  ResMsg.error("删除失败");
        }
    }

    @GetMapping("/detail")
    public String detail(Integer id){
        Banner banner ;
        try{
            banner = bannerService.detail(id);
        }catch (Exception e){
            return ResMsg.error("轮播图详情加载失败",e);
        }
        return ResMsg.success("轮播图详情加载成功",banner);
    }

    @PostMapping("/update")
    public String update(@RequestBody Banner banner){
        int i = bannerService.update(banner);
        if(i>0){
            return ResMsg.success("修改成功");
        }else{
            return ResMsg.error("修改失败");
        }
    }
}
